# 此时的if条件已经变成了True和False
from not_so_free_marker import statics
from not_so_free_marker.enums import MarkTypeEnum
from not_so_free_marker.models import RegMatchIf, RegMatch


def process(template_str: str):
    return __private_process_if(template_str);


def get_if_tags(template_str: str):
    if_tags = []
    for h in statics.regIfHead.finditer(template_str):
        reg_item = RegMatchIf()
        reg_item.position = h.start(0)
        reg_item.position_end = h.end(0)
        reg_item.type = MarkTypeEnum.REG_IF_HEAD
        reg_item.tagStr = h.group(0)
        reg_item.express = h.group(1)
        if_tags.append(reg_item)

    for h in statics.regElseIf.finditer(template_str):
        reg_item = RegMatchIf()
        reg_item.position = h.start(0)
        reg_item.position_end = h.end(0)
        reg_item.type = MarkTypeEnum.REG_IF_ELSEIF
        reg_item.tagStr = h.group(0)
        reg_item.express = h.group(1)
        if_tags.append(reg_item)

    for t in statics.regElse.finditer(template_str):
        reg_item = RegMatch()
        reg_item.position = t.start(0)
        reg_item.position_end = t.end(0)
        reg_item.type = MarkTypeEnum.REG_IF_ELSE
        reg_item.tagStr = t.group(0)
        if_tags.append(reg_item)

    for t in statics.regIfTail.finditer(template_str):
        reg_item = RegMatch()
        reg_item.position = t.start(0)
        reg_item.position_end = t.end(0)
        reg_item.type = MarkTypeEnum.REG_IF_TAIL
        reg_item.tagStr = t.group(0)
        if_tags.append(reg_item)
    # 按照位置排序
    if_tags.sort(key=lambda a: a.position)

    return if_tags


def __private_process_if(template_str: str):
    processed = []
    tags = get_if_tags(template_str)
    tag_group = __private_group_tags(tags)
    last_index = 0
    for group in tag_group:
        start_index, end_index, true_index, true_index_end = __private_get_group_index(group)
        # 加入之前的部分
        processed.append(template_str[last_index:start_index])
        if true_index > -1:
            processed.append(template_str[true_index:true_index_end])
        last_index = end_index
    processed.append(template_str[last_index:])

    return "".join(processed)


# 把所有if标签按照if...else...endif分组.每组是一整个if语句
def __private_group_tags(tag_list: list):
    tag_group = []
    temp_tag_list = []
    for tag in tag_list:
        temp_tag_list.append(tag)

        if tag.type == MarkTypeEnum.REG_IF_TAIL:
            tag_group.append(temp_tag_list)
            temp_tag_list = []
    return tag_group


# 判断哪个比较分支将会被返回.
# 返回if的tag标签组中,为真的索引,和整个group开始结束索引.
def __private_get_group_index(tag_group: list):
    group_lastidx = len(tag_group) - 1
    start_index = tag_group[0].position
    end_index = tag_group[group_lastidx].position_end
    true_index = -1
    true_index_end = -1
    group_else_index = -1

    loop_index = 0
    for tag in tag_group:
        # 如果是else,记录下位置
        if tag.type == MarkTypeEnum.REG_IF_ELSE:
            group_else_index = loop_index

        # 如果是true.(只有elseif 和if才会进入这个分支)
        if hasattr(tag, "express") and tag.express.strip() == "True":
            true_index = tag.position_end
            true_index_end = tag_group[loop_index + 1].position

        loop_index += 1

    # 有true
    if true_index >= 0:
        return start_index, end_index, true_index, true_index_end
    # 没有true,但是有else
    if group_else_index >= 0:
        true_index = tag_group[group_else_index].position_end
        true_index_end = tag_group[group_else_index + 1].position
    # 没有符合条件的,注意此时trueindex, true_index_end=-1
    return start_index, end_index, true_index, true_index_end
